package com.wgts.wgts.mvp.view.activity.mycenter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.wgts.wgts.R;
import com.wgts.wgts.tools.MoHuGlide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
 *
 *
 *   data:2018/9/2817:39
 *
 */public class IdeaBackRecycleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int BODY_TYPE = 0x00001;
    private static final int FOOT_TYPE = 0x00002;

    private deleteImageOnClick deleteImageOnClick;
    private addImageOnClick addImageOnClick;

    private Context context;
    private List<Uri> list;

    public IdeaBackRecycleAdapter(Context context, List<Uri> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == FOOT_TYPE) {
            return new footViewHold(LayoutInflater.from(context).inflate(R.layout.idea_back_recycle_foot_view, viewGroup, false));
        }
        return new bodyViewHold(LayoutInflater.from(context).inflate(R.layout.idea_back_recycle_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, final int i) {
        if (viewHolder instanceof bodyViewHold) {
            MoHuGlide.getInstance().glideImg(context, list.get(i).toString(), ((bodyViewHold) viewHolder).ideaBackImage);
            ((bodyViewHold) viewHolder).deleteIdeaBackImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (deleteImageOnClick != null){
                        deleteImageOnClick.deleteOnclick(((bodyViewHold) viewHolder).deleteIdeaBackImage, i);
                    }
                }
            });
        } else if (viewHolder instanceof footViewHold) {
            if (i == 5){
                ((footViewHold)viewHolder).itemView.setVisibility(View.GONE);
            }else {
                ((footViewHold)viewHolder).itemView.setVisibility(View.VISIBLE);
                ((footViewHold) viewHolder).addIdeaBackImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (addImageOnClick != null){
                            addImageOnClick.addOnclick(((footViewHold) viewHolder).addIdeaBackImage, i);
                        }
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        if (list.size() == 5){
            return list.size();
        }else {
            return list.size() + 1;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (list.size() == 0){
            return FOOT_TYPE;
        }else {
            if (position == (list.size())) {
                return FOOT_TYPE;
            } else {
                return BODY_TYPE;
            }
        }
    }

    public class footViewHold extends RecyclerView.ViewHolder {
        @BindView(R.id.add_idea_back_image)
        ImageView addIdeaBackImage;
        public View itemView;
        public footViewHold(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }
    }

    public class bodyViewHold extends RecyclerView.ViewHolder {
        @BindView(R.id.idea_back_image)
        ImageView ideaBackImage;
        @BindView(R.id.delete_idea_back_image)
        ImageView deleteIdeaBackImage;

        public bodyViewHold(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface deleteImageOnClick{
        void deleteOnclick(View view, int position);
    }

    public void setDeleteImageOnClick(deleteImageOnClick deleteImageOnClick){
        this.deleteImageOnClick = deleteImageOnClick;
    }

    public interface addImageOnClick{
        void addOnclick(View view, int position);
    }

    public void setAddImageOnClick(addImageOnClick addImageOnClick){
        this.addImageOnClick = addImageOnClick;
    }

    public void setDate(List<Uri> date){
        list = date;
        notifyDataSetChanged();
    }
}
